package com.weitu.permission.department.service.impl;

import com.weitu.base.entity.Tree;
import com.weitu.permission.department.entity.Department;
import com.weitu.permission.department.mapper.DepartmentMapper;
import com.weitu.permission.department.service.DepartmentService;
import com.weitu.permission.departmentDuty.mapper.DepartmentDutyMapper;
import com.weitu.permission.duty.entity.Duty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    private final DepartmentMapper departmentMapper;

    private final DepartmentDutyMapper departmentDutyMapper;

    @Autowired
    public DepartmentServiceImpl(DepartmentMapper departmentMapper, DepartmentDutyMapper departmentDutyMapper) {
        this.departmentMapper = departmentMapper;
        this.departmentDutyMapper = departmentDutyMapper;
    }

    @Override
    public List<Department> findByParentId(String parentId) {
        return departmentMapper.findByParentId(parentId);
    }

    @Override
    public String findCodeByParentId(String parentId) {

        List<Map> mapList = departmentMapper.findCodeByParentId(parentId);
        if (!mapList.isEmpty()) {
            if (mapList.size() > 1) {
                Integer returnCode = 0;
                for (int i = 0; i < mapList.size() - 1; i++) {
                    int j = i + 1;
                    Integer iCode = Integer.valueOf(mapList.get(i).get("code").toString());
                    Integer jCode = Integer.valueOf(mapList.get(j).get("code").toString());

                    if (jCode != 0 && iCode != 0 && jCode - iCode > 1) {
                        returnCode = iCode + 1;
                    }
                }
                if (returnCode == 0) {
                    returnCode = Integer.valueOf(mapList.get(mapList.size() - 1).get("code").toString()) + 1;
                }
                return "00" + returnCode.toString();
            } else {
                Integer iCode = Integer.valueOf(mapList.get(0).get("code").toString()) + 1;
                return "00" + iCode.toString();
            }

        } else {
            return departmentMapper.findById(parentId).getCode() + "001";
        }
    }

    @Override
    public Department findById(String id) {
        return departmentMapper.findById(id);
    }

    @Override
    public Department findByCode(String code) {
        return departmentMapper.findByCode(code);
    }

    @Override
    public List<Department> findByUserId(String userId) {
        return departmentMapper.findByUserId(userId);
    }

    @Override
    public Department findByCodeAndId(String code, String id) {
        return departmentMapper.findByCodeAndId(code, id);
    }


    @Override
    public List<Tree> getDepartmentTree(String parentId) {
        List<Department> parentDepartmentList = findByParentId(parentId);
        List<Department> childrenDepartmentList;
        List<Tree> parentTreeList = new ArrayList<Tree>();
        Tree tree;

        for (Department department : parentDepartmentList) {
            String id = department.getId();
            String name = department.getName();
            String code = department.getCode();
            Map map = new HashMap();
            map.put("code", code);
            tree = new Tree();
            tree.setId(id);
            tree.setText(name);
            tree.setAttributes(map);
            childrenDepartmentList = findByParentId(id);
            if (childrenDepartmentList.size() > 0) {
                if (!parentId.equals("0")) {
                    tree.setState("closed");
                }
                tree.setChildren(getDepartmentTree(id));
            }
            parentTreeList.add(tree);

        }
        return parentTreeList;
    }

    @Override
    public List<Tree> getDeptAndDutyTreeByUserId(String userId) {
        //根据userId 获得部门
        List<Tree> panentTreeList = new ArrayList<Tree>();
        List<Tree> childrenTreeList;

        List<Department> departmentList = findByUserId(userId);

        Tree deptTree;
        Tree dutyTree;

        Map map;

        for (Department department : departmentList) {
            String deptId = department.getId();
            String deptName = department.getName();
            deptTree = new Tree();
            deptTree.setId(deptId);
            deptTree.setText(deptName);
            map = new HashMap();
            map.put("deptId", deptId);
            map.put("flag", "true");
            List<Duty> dutyList = departmentDutyMapper.findByParam(map);
            childrenTreeList = new ArrayList<Tree>();
            for (Duty duty : dutyList) {
                String dutyId = duty.getId();
                String dutyName = duty.getName();
                dutyTree = new Tree();
                dutyTree.setId(deptId + "-" + dutyId);
                dutyTree.setText(dutyName);
                childrenTreeList.add(dutyTree);

            }
            deptTree.setChildren(childrenTreeList);
            panentTreeList.add(deptTree);
        }

        return panentTreeList;
    }

    @Override
    public List<Department> findByParam(Map paramMap) {
        return departmentMapper.findByParam(paramMap);
    }

    @Override
    public Integer findByParamCount(Map paramMap) {
        return departmentMapper.findByParamCount(paramMap);
    }

    @Transactional
    @Override
    public Integer add(Map paramMap) {

        return departmentMapper.add(paramMap);
    }

    @Transactional
    @Override
    public Integer updateById(Map paramMap) {
        //递归修改部门的时候同时修改子级部门的code

        recursionUpdateDepartmentCode(paramMap);

        return departmentMapper.updateById(paramMap);
    }

    @Override
    public List<String> selectNamesByIds(List<String> ids) {
        return departmentMapper.selectNamesByIds(ids);
    }

    private void recursionUpdateDepartmentCode(Map paramMap){
        String id=paramMap.get("id").toString();
        String code=paramMap.get("code").toString();
        //查询当前部门的子级部门
        List<Department> departmentList= departmentMapper.findByParentId(id);
        Map sqlMap=null;
        for (Department department :departmentList){
            sqlMap=new HashMap();
            String dId=department.getId();
            String dCode=department.getCode();
            dCode=code+dCode.substring(dCode.length()-3);
            sqlMap.put("id",dId);
            sqlMap.put("code",dCode);
            departmentMapper.updateById(sqlMap);
            recursionUpdateDepartmentCode(sqlMap);
        }
    }
}
